package com.rtx.edi.log.service.impl;

import java.lang.reflect.Field;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.edi.log.bean.EdiContentLog;
import com.rtx.edi.log.bean.EdiLog;
import com.rtx.edi.log.mapper.EdiContentLogMapper;
import com.rtx.edi.log.mapper.EdiLogMapper;
import com.rtx.edi.log.service.EdiLogService;

@Service
public class EdiLogServiceImpl extends ServiceImpl<EdiLogMapper, EdiLog> implements EdiLogService{
	
	@Autowired
	EdiContentLogMapper ediContentLogMapper;

	@Override
	public void addlogandcontent(String datasourcekey,EdiLog log) {
		baseMapper.insert(log);
		ediContentLogMapper.insert(log.getEdiContentLog());
	}

	@Override
	public void selectPageList(Page<EdiLog> pageParam, EdiLog dto) {
		LambdaQueryWrapper<EdiLog> logqw = Wrappers.lambdaQuery();
		logqw.orderByAsc(EdiLog::getId);
		logqw.like(dto.getEdiapiname() !=null, EdiLog::getEdiapiname,dto.getEdiapiname());
		logqw.like(dto.getLogkey() !=null, EdiLog::getLogkey,dto.getLogkey());
		baseMapper.selectPage(pageParam, logqw);
	}

	@Override
	public EdiContentLog selectByLogno(EdiLog log){
		LambdaQueryWrapper<EdiContentLog> logqw = Wrappers.lambdaQuery();
		logqw.eq(EdiContentLog::getLogno, log.getLogno());
		EdiContentLog logdata = ediContentLogMapper.selectOne(logqw);
		try {
			for(Field field:EdiContentLog.class.getDeclaredFields()) {
				field.setAccessible(true);
				Object data = field.get(logdata);
				if(null == data) {
					field.set(logdata, "");
				}
			}
		} catch (Exception e) {
		}

		return logdata;
	}
}
